using System;
using System.Collections.Generic;
using DA.AnimGraph;
using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {

    [NodeMenuItem(typeof(AGANode_Sequence), "Sequence")]
    internal class NodeView_Sequence : NodeViewBase {
        public override string titleText => "Sequence";

        protected override NodeViewInspectorSourceBase dataSource => inspectorData;
        NodeViewInspector_Sequence inspectorData;

        internal override void Init(AGAHandle_Node handle, AnimGraphView graphView, AGEEnvironment ageContext) {
            inspectorData = new NodeViewInspector_Sequence();
            inspectorData.SetHandle(handle);
            base.Init(handle, graphView, ageContext);
        }

    }

    internal class NodeViewInspector_Sequence : NodeViewInspectorSourceBase {

        [InspectorField(label: "播放速度")] public AGAFieldModel_FloatLink playbackSpeed;
        [InspectorField(label: "自动首尾相接(启用时无需再填写offset等参数)")]
        public RefField<bool> isAutoConnect;
        [InspectorField(isList: true)] public List<AGAFieldModel_SequenceClipSlice> clipSlices;
        [InspectorField(tag: "sort")] public Action sort;

        ListView clipSlicesListView;

        protected override void LoadFromModel() {
            var model = handleKeeper.handle.data.modelProperty as AGANodeModel_Sequence;
            clipSlices = model.clipSlices;
            if (model.playbackSpeed == null) {
                model.playbackSpeed = new AGAFieldModel_FloatLink() { defaultValue = 1f };
            }
            playbackSpeed = model.playbackSpeed;
            isAutoConnect = new RefField<bool>(model.isAutoConnect);

            sort = Sort;
        }

        protected override void SaveToModel() {
            //排序(会引起失焦)
            //clipSlices.Sort((x, y) => {
            //    return (x.start + x.length).CompareTo(y.start + y.length);
            //});
            //clipSlicesListView.Rebuild();
            var model = handleKeeper.handle.data.modelProperty as AGANodeModel_Sequence;
            model.isAutoConnect = isAutoConnect.value;
            handleKeeper.handle.SetDirty();
        }

        public void Sort() {
            if (isAutoConnect.value) return;
            AGAFieldModel_SequenceClipSlice.Sort(clipSlices);
            clipSlicesListView.Rebuild();
        }

        protected override void OnDrawUIFinish(AGEContext ageContext) {
            clipSlicesListView = inspectorWidgets["clipSlices"] as ListView;

            clipSlicesListView.showAddRemoveFooter = true;
            clipSlicesListView.reorderable = false;
            clipSlicesListView.fixedItemHeight = 85;

            //void LocalSort() {
            //    clipSlices.Sort((x, y) => {
            //        return (x.start + x.length).CompareTo(y.start + y.length);
            //    });
            //    clipSlicesListView.Rebuild();
            //}

            clipSlicesListView.itemsAdded += indices => {
                foreach (var index in indices) {
                    var newItem = new AGAFieldModel_SequenceClipSlice();
                    clipSlices[index] = newItem;
                }
                //LocalSort();
                SaveToModel();
            };
            clipSlicesListView.itemsRemoved += indices => {
                // 引用直接关联，等待删除即可(删除不会引起顺序变化)
                foreach (var index in indices) {
                    ageContext.resourceManager.TryRemoveResource(clipSlices[index].clip.clipKey);
                }
            };
            // 失去焦点触发复杂，需要一个合适调用排序的地方
            //clipSlicesListView.RegisterCallback<FocusOutEvent>(evt => {
            //    LocalSort();
            //});

            clipSlicesListView.Rebuild();
        }

    }

}
